<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Runloop与NSTimer</title>
  
  <link href="../css/semantic/semantic.min.css" rel="stylesheet">
  <link href="../css/markdown.css" rel="stylesheet">
  <link href="../css/wtxynavigation.css" rel="stylesheet">
  <script src="../js/jquery.min.js"></script>
  <script type="text/javascript" src="../js/include.js"></script>

  <style>
    body{padding-top:0;background:#f7f7f7}
    body>.ui.container{width:100%;padding:0}
    #project-title{background:#6e6e6e;width:100%;line-height:3.572rem;color:#fff;font-size:1.2rem}
    #project-title .title-wrap{font-weight:700;padding:0 12px;width:100%;max-width:1366px!important;min-width:900px;margin:0 auto}
    #project-title .left,#project-title right{display:inline-block}
    #project-title .right{float:right}
    #project-title .right .wiki-actions .menu{left:auto;right:0;margin-top:.4em!important}
    #wiki-preview-container{width:100%;max-width:1366px!important;min-width:900px;padding:0;border:1px solid #e3e9ed;border-top:0}
    #wiki-preview>.ui.segment{margin-top:0;padding:1rem;padding-left:0;border:0}
    .hover-places{display:inline-block}
    #page-detail{width:60%;margin:0 auto}
    #page-detail .title{font-weight:700;font-size:24px;padding:12px 0;border-bottom:1px solid #ccc}
    #page-detail .content{margin-top:15px}
    .content p{word-wrap:break-word}
    #wiki-parent,#wiki-title,#wikis{display:none}
    .ui.grid{padding:0!important}
    #ex-all{cursor:pointer;color:#537c8d}
  </style>
</head>
<body>
  <div class="ui container">
    <div id="project-title">
      <div class="title-wrap">
        <div class="left">
          <i class="icon eye"></i>
          文档预览:
          mianmSDK
        </div>
        <div class="right">
          Export by Gitee
        </div>
      </div>
    </div>

    <div class="ui container" id="wiki-preview-container">
      <div id="wiki-preview">
        <div class="ui segment">
          <div id="page-detail" class="markdown-body">
            <div class='title'>34.Runloop与NSTimer</div><div class='content'><h1>
<a id="runloop与nstimer" class="anchor" href="#runloop%E4%B8%8Enstimer"></a>Runloop与NSTimer</h1>
<p><strong>关键词</strong>:</p>
<ol>
<li>CFRunLoopAddTimer</li>
<li>CFSetApplyFunction</li>
<li><code>__CFRunLoopAddItemToCommonModes</code></li>
</ol>
<h1>
<a id="cfrunloopaddtimer" class="anchor" href="#cfrunloopaddtimer"></a>CFRunLoopAddTimer</h1>
<p><a href="https://opensource.apple.com/source/CF/CF-635/CFRunLoop.c.auto.html">看源代码CFRunLoopAddTimer</a></p>
<pre><code>

void CFRunLoopAddTimer(CFRunLoopRef rl, CFRunLoopTimerRef rlt, CFStringRef modeName) 
;

</code></pre>
<ul>
<li>
<code>CFRunLoopRef rl</code>: Runloop</li>
<li>
<code>CFRunLoopTimerRef rlt</code>: Timer</li>
<li>
<code>CFStringRef modeName</code>: 模式的名称</li>
</ul>
<p><img src="images/runloopT01.png" alt="runloopT01"></p>
<ol>
<li>如果当前放进来的是<code>kCFRunLoopCommonModes</code>
</li>
<li>首先就提取<code>Runloop</code>的<code>_commonModes</code>(<code>CFSetRef set</code>一堆字符串元素),看看有没有</li>
<li>再看一下<code>_commonModeItems</code>这玩意存不存在</li>
<li>上面两个都看看, 不存在的话就创建一个新的.</li>
<li>然后呢就是把<code>Timer</code>加到<code>_commonModeItems</code>这个集合中</li>
<li>然后就把我们的<code>Runloop</code>和<code>Timer</code>封装成一个<code>context</code>就是这句<code>CFTypeRef context[2] = {rl, rlt};</code>
</li>
<li>然后对<code>_commonModes</code>这个集合中的每一个元素调用<code>__CFRunLoopAddItemToCommonModes</code>这么一个函数,就是这句<code>CFSetApplyFunction(set, (__CFRunLoopAddItemToCommonModes), (void *)context);</code>
</li>
<li>然后再带着真正的<code>mode</code>回到<code>CFRunLoopAddTimer</code>函数</li>
<li>实际上就是为每个<code>mode</code>添加了<code>Timer</code>
</li>
</ol>
<p><img src="images/runloopT02.png" alt="runloopT02"></p>
<h3>
<a id="1" class="anchor" href="#1"></a>1.</h3>
<h1>
<a id="具体还是要看cf源码" class="anchor" href="#%E5%85%B7%E4%BD%93%E8%BF%98%E6%98%AF%E8%A6%81%E7%9C%8Bcf%E6%BA%90%E7%A0%81"></a>具体还是要看CF源码</h1></div>
          </div>
        </div>
      </div>
      </div>
  </div>
</body>
</html>